package com.ztom.v2;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @author ZhangTao
 */
public class Code56Subsets {

    public List<List<Integer>> subsets(int[] nums) {
        if (nums == null || nums.length == 0) {
            return new ArrayList<>();
        }
        List<List<Integer>> res = new ArrayList<>();
        dfs(nums, 0, res, new LinkedList<>());
        return res;
    }

    public void dfs(int[] nums, int index, List<List<Integer>> res, Deque<Integer> path) {
        if (index == nums.length) {
            res.add(new ArrayList<>(path));
            return;
        }
        // 取当前的数
        path.addLast(nums[index]);
        dfs(nums, index + 1, res, path);
        path.pollLast();

        // 不取当前的数
        dfs(nums, index + 1, res, path);
    }
}
